<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="example" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="example" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data)"
                        >编辑子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="del(data.id)"
                        >删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      v-if="showDialog"
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
    >
      <depart-dialog
        :origin-list="originList"
        :pid="pid"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arraytoTree } from '@/utils'
import departDialog from './departDialog.vue'
export default {
  components: { departDialog },
  data() {
    return {
      originList: [],
      isEdit: false,
      showDialog: false,
      pid: '',
      list: [
        // {
        //   label: '总裁办'
        // },
        // {
        //   label: '财务部',
        //   children: [
        //     {
        //       label: '财务核算部'
        //     },
        //     {
        //       label: '税务核算部'
        //     }
        //   ]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)
      res.data.depts.shift()
      this.originList = res.data.depts.map((item) => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
      this.list = arraytoTree(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    hAdd(id) {
      this.showDialog = true
      this.isEdit = false
      this.pid = id
      console.log(id)
    },
    hEdit(data) {
      this.isEdit = true
      this.showDialog = true
      this.pid = data.id
    },
    async del(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      this.loadDepartments()
    },
    hSuccess() {
      this.showDialog = false
      this.$message.success('添加成功')
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    }
  }
}
</script>
